package LeetCode热题HOT100;

public class p35搜索插入位置 {
    public int searchInsert(int[] nums, int target) {
        //暴力求解 的时间复杂度为 O(n)
        //二分查找 的时间复杂度为 O(logn)，前提：数组有序
        int left=0;
        int right=nums.length-1;
        int mid=0;

        while (left<=right) {
            mid=(left+right)/2;
            if (nums[mid]==target) {
                return mid;
            }else if (nums[mid]>target) {
                right=mid-1;
            }else {
                left=mid+1;
            }
        }
        return left;
    }
}
